package com.tanhua.dubbo.api;

import com.tanhua.model.db.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @auther : 白月尘
 * @date : 16:15  2021/10/25
 */
@DubboService
public class FocusUserApiImpl implements FocusUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveFocusUser(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    @Override
    public void deleteFocusUser(long uid, long userId) {
        Criteria criteria = Criteria.where("userId").is(uid).and("followUserId").is(userId);
        Query query = Query.query(criteria);
        if (mongoTemplate.exists(query,FocusUser.class)){
            mongoTemplate.remove(query,FocusUser.class);
        }else {
            throw new RuntimeException();
        }
    }
}
